<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }


        .box {
            width: 200px;
            height: 200px;
            background-color: #6ff;
            margin: 50px;
            overflow: hidden;
        }

        .sub {
            width: 100px;
            height: 100px;
            background-color: pink;
            margin: 50px;
            /* 点透属性 -> 设置了此属性的元素不再作为鼠标事件的事件源, 点击透过该元素 */
            pointer-events: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="sub"></div>
    </div>
</body>
<script>
    // 如何始终获取鼠标相对于box的偏移?

    // 1.  鼠标相对于文档的偏移 - box相对于文档的偏移
    // var posX = e.pageX - box.offsetLeft;
    // var posY = e.pageY - box.offsetTop;

    // 2. 给sub设置css点透属性 pointer-events: none;  设置了此属性的元素不再作为鼠标事件的事件源, 点击透过该元素 

    var box = document.getElementsByClassName("box")[0];

    box.onmousedown = function (e) {  // 形参 e  evt  event  (常规浏览器)
        var e = e || window.event;
        // console.log(e);
        // console.log(e.target, e.offsetX, e.offsetY);

        var posX = e.pageX - box.offsetLeft;
        var posY = e.pageY - box.offsetTop;
        console.log(posX, posY);


        var posX = e.offsetX;
        var posY = e.offsetY;
        console.log(e.target, posX, posY);

    }








</script>

</html>